/*
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.autogroup;

import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.instance.instancereward.PvPArenaReward;
import com.aionengine.gameserver.network.aion.serverpackets.SM_AUTO_GROUP;
import com.aionengine.gameserver.services.AutoGroupService;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author xTz
 */
public class AutoPvPFFAInstance extends AutoInstance {

    @Override
    public AGQuestion addPlayer(Player player, SearchInstance searchInstance) {
        super.writeLock();
        try {
            if (!satisfyTime(searchInstance) || (players.size() >= agt.getPlayerSize())) {
                return AGQuestion.FAILED;
            }
            players.put(player.getObjectId(), new AGPlayer(player));
            return instance != null ? AGQuestion.ADDED : (players.size() == agt.getPlayerSize() ? AGQuestion.READY : AGQuestion.ADDED);
        } finally {
            super.writeUnlock();
        }
    }

    @Override
    public void onPressEnter(Player player) {
        super.onPressEnter(player);
        if (agt.isPvPFFAArena() || agt.isPvPSoloArena() || agt.isGloryArena()) {
            long size = 1;
            int itemId = 186000135;
            if (agt.isGloryArena()) {
                size = 3;
                itemId = 186000185;
            }
            if (!decrease(player, itemId, size)) {
                players.remove(player.getObjectId());
                PacketSendUtility.sendPacket(player, new SM_AUTO_GROUP(instanceMaskId, 5));
                if (players.isEmpty()) {
                    AutoGroupService.getInstance().unRegisterInstance(instance.getInstanceId());
                }
                return;
            }
        }
        ((PvPArenaReward) instance.getInstanceHandler().getInstanceReward()).portToPosition(player);
        instance.register(player.getObjectId());
    }

    @Override
    public void onLeaveInstance(Player player) {
        super.unregister(player);
    }

}
